package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.dao.TagLinkDao;
import com.xbongbong.saas.domain.entity.TagLinkEntity;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.enums.TagEnum;
import com.xbongbong.saas.enums.TagTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.TagLinkModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 *
 * @author 魏荣杰
 * @date 2019/2/27 13:54
 * @since v1.0
 * @version v1.0
 */
@Service("tagLinkModel")
public class TagLinkModelImpl implements TagLinkModel {
    private static final Logger LOG = LoggerFactory.getLogger(TagLinkModelImpl.class);

    @Resource
    private TagLinkDao tagLinkDao;

    @Override
    public Integer insert(TagLinkEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        return tagLinkDao.insert(entity);
    }

    @Override
    public Integer update(TagLinkEntity entity) throws XbbException {
        return tagLinkDao.update(entity);
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return tagLinkDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return tagLinkDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer save(TagLinkEntity tagLinkEntity) throws XbbException {
        if (tagLinkEntity.getId() == null || Objects.equals(0, tagLinkEntity.getId())) {
            return insert(tagLinkEntity);
        }
        return update(tagLinkEntity);
    }

    @Override
    public void deleteReturnPublicTags(String corpid, Integer tagType) {
        try {
            tagLinkDao.deleteTagTypeTags(tagType, corpid);
        } catch (Exception e) {
            LOG.error("标签删除失败，corpid：" + corpid, e);
        }
    }

    @Override
    public void batchInsert(ArrayList<TagLinkEntity> tagLinkList) {
        tagLinkDao.batchInsert(tagLinkList);
    }

    @Override
    public void batchUpdate(ArrayList<TagLinkEntity> tagLinkList) {
        for(TagLinkEntity tagLink : tagLinkList){
            try {
                update(tagLink);
            } catch (XbbException e) {
                LOG.error("更新标签失败！", e);
            }
        }
    }

    @Override
    public List<TagLinkEntity> getCompanyTagLinksByRefType(String corpid, Integer tagType, Collection<Long> refIdIn) {
        if (tagType == null || refIdIn == null || refIdIn.size() == 0) {
            return new ArrayList<>();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("tagType", tagType);
        params.put("corpid", corpid);
        params.put("refIdIn", refIdIn);
        return findEntitys(params);
    }

    @Override
    public void deleteTagByTagTypeAndRefIdIn(String corpid, Integer tagType, List<Long> refIdIn) {
        if (tagType == null || refIdIn == null) {
            return;
        }
        tagLinkDao.deleteTagByTagTypeAndRefIdIn(corpid, tagType, refIdIn);
    }

    @Override
    public void setPublicTagLink4Customer(List<CustomerEntityExt> customerList) {
        if (customerList == null || customerList.isEmpty()) {
            return ;
        }
        String corpid = customerList.get(0).getCorpid();
        if (StringUtil.isEmpty(corpid)) {
            return ;
        }

        Integer refType = XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode();

        List<Long> refIdIn = new ArrayList<>();
        for (CustomerEntityExt customer : customerList) {
            refIdIn.add(customer.getId());
        }
        List<TagLinkEntity> tagLinkList = getCompanyTagLinksByRefType(corpid, refType, refIdIn);
        Map<Long, List<TagLinkEntity>> tagLinkMap = new HashMap<>();
        for(TagLinkEntity tagLink : tagLinkList){
            Long refId = tagLink.getRefId();
            List<TagLinkEntity> tagLinks = tagLinkMap.get(refId);
            if(tagLinks == null){
                tagLinks = new ArrayList<>();
            }
            tagLinks.add(tagLink);
            tagLinkMap.put(refId, tagLinks);
        }
        for (CustomerEntityExt customer : customerList) {
            Long customerId = customer.getId();
            List<TagLinkEntity> tagLinks = tagLinkMap.get(customerId);
            customer.setTagLinkList(tagLinks);
        }
    }

    @Override
    public Map<Long,String> getPublicTag(String corpid, List<Long> dataIdList) {
        List<TagLinkEntity> tagLinkList = getCompanyTagLinksByRefType(corpid, XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode(), dataIdList);
        Map<Long,String> publicTagMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (TagLinkEntity entity : tagLinkList){
            publicTagMap.put(entity.getRefId(),entity.getMemo());
        }
        return publicTagMap;
    }

    @Override
    public Map<Long,String> getTransferTag(String corpid, List<Long> dataIdList,Integer refType) {
        List<TagLinkEntity> tagLinkList = getCompanyTagLinksByRefType(corpid, refType, dataIdList);
        Map<Long,String> publicTagMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (TagLinkEntity entity : tagLinkList){
            publicTagMap.put(entity.getRefId(),entity.getMemo());
        }
        return publicTagMap;
    }


    @Override
    public Map<Long,String> getCluePublicTag(String corpid, List<Long> dataIdList) {
        List<TagLinkEntity> tagLinkList = getCompanyTagLinksByRefType(corpid, XbbRefTypeEnum.CLUE_PUBLIC.getCode(), dataIdList);
        Map<Long,String> publicTagMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (TagLinkEntity entity : tagLinkList){
            publicTagMap.put(entity.getRefId(),entity.getMemo());
        }
        return publicTagMap;
    }

    @Override
    public Map<Long, TagLinkEntity> getPublicTagLink(String corpid, Collection<Long> dataIdList, Integer businessType) {
        List<TagLinkEntity> tagLinkList = getCompanyTagLinksByRefType(corpid, businessType, dataIdList);
        Map<Long, TagLinkEntity> publicTagMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (TagLinkEntity entity : tagLinkList){
            publicTagMap.put(entity.getRefId(), entity);
        }
        return publicTagMap;
    }

    @Override
    public TagLinkEntity getTagByTagType(String corpid, Integer tagType, Long refId) {
        if (tagType == null || refId == null) {
            return null;
        }
        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", corpid);
        params.put("tagType", tagType);
        params.put("refId", refId);
        params.put("limitNum", 1);
        List<TagLinkEntity> tagLink = findEntitys(params);
        return tagLink.size() == 0 ? null : tagLink.get(0);
    }

    @Override
    public List<Long> getPublicCustomerIdList(String corpid, Integer businessType) {
        Integer subBusinessType = XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode();
        Integer tagId = TagEnum.TAG_RETURN_PUBLIC.getId();
        if (Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode())) {
            subBusinessType = XbbRefTypeEnum.CLUE_PUBLIC.getCode();
            tagId = TagEnum.TAG_RETURN_PUBLIC_CLUE.getId();
        }
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("tagType", subBusinessType);
        param.put("tagId", tagId);
        return getDataIdList(param);
    }

    /**
     * 获取refId
     * @param param
     * @return
     */
    private List<Long> getDataIdList(Map<String,Object> param) {
        return tagLinkDao.getDataIdList(param);
    }
}
